<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <el-menu
        :default-active="$route.path"
        class="el-menu-vertical"
        background-color="#304156"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <template v-if="isAdmin">
          <el-menu-item index="/admin/dashboard">
            <i class="el-icon-s-home"></i>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/user-list">
            <i class="el-icon-user"></i>
            <span>用户列表</span>
          </el-menu-item>
          <el-menu-item index="/announcement">
            <i class="el-icon-bell"></i>
            <span>公告信息</span>
          </el-menu-item>
        </template>
        <template v-else>
          <el-menu-item index="/dashboard">
            <i class="el-icon-s-home"></i>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/bill">
            <i class="el-icon-money"></i>
            <span>收支账单</span>
          </el-menu-item>
          <el-menu-item index="/bank-card">
            <i class="el-icon-bank-card"></i>
            <span>银行卡管理</span>
          </el-menu-item>
          <el-menu-item index="/wish-list">
            <i class="el-icon-star-on"></i>
            <span>心愿目标</span>
          </el-menu-item>
          <el-menu-item index="/memo">
            <i class="el-icon-notebook-2"></i>
            <span>备忘录</span>
          </el-menu-item>
          <el-menu-item index="/schedule">
            <i class="el-icon-calendar"></i>
            <span>日程管理</span>
          </el-menu-item>
          <el-submenu index="/blog">
            <template slot="title">
              <i class="el-icon-reading"></i>
              <span>博客管理</span>
            </template>
            <el-menu-item index="/blog/index">
              <i class="el-icon-document"></i>
              <span>文章管理</span>
            </el-menu-item>
            <el-menu-item index="/blog/category">
              <i class="el-icon-folder"></i>
              <span>分类管理</span>
            </el-menu-item>
          </el-submenu>
          <el-menu-item index="/password/index">
            <i class="el-icon-lock"></i>
            <span>密码管理</span>
          </el-menu-item>
          <el-menu-item index="/site/index">
            <i class="el-icon-link"></i>
            <span>站点管理</span>
          </el-menu-item>
          <el-menu-item index="/announcement">
            <i class="el-icon-bell"></i>
            <span>公告信息</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ currentUser?.username || '未登录' }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="profile">个人信息</el-dropdown-item>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Layout',
  computed: {
    ...mapGetters(['currentUser', 'isAdmin'])
  },
  async created() {
    if (!this.currentUser && this.$store.state.token) {
      try {
        const response = await this.$http.get('/user/info', {
          params: {
            token: this.$store.state.token
          }
        })
        if (response.data.code === 20000) {
          this.$store.commit('SET_USER', response.data.data)
        } else {
          this.$router.push('/login')
        }
      } catch (error) {
        this.$message.error('获取用户信息失败')
        this.$router.push('/login')
      }
    }
  },
  methods: {
    async handleCommand(command) {
      if (command === 'logout') {
        try {
          await this.$http.post('/user/logout', null, {
            headers: {
              'X-Token': this.$store.state.token
            }
          })
          this.$store.dispatch('logout')
          this.$message.success('退出登录成功')
          this.$router.push('/login')
        } catch (error) {
          this.$message.error('退出登录失败')
        }
      } else if (command === 'profile') {
        if (this.$route.path !== '/profile') {
          this.$router.push('/profile')
        }
      }
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-aside {
  background-color: #304156;
}

.el-menu {
  border-right: none;
}

.el-header {
  background-color: #fff;
  color: #333;
  line-height: 60px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.header-right {
  float: right;
  margin-right: 20px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 